Skip to content

This--keyword#15

Open
Butcher3Years wants to merge 16 commits intomainfrom
this--keyword
Open

This--keyword#15
Butcher3Years wants to merge 16 commits intomainfrom
this--keyword

Conversation

@Butcher3Years
Copy link
Copy Markdown
Owner

The this Keyword in C++ – Pointer to the Current Object

The this keyword is a special pointer that every non-static member function (including constructors) gets automatically.
It points to the current object you're working inside.

Your Code Example

class Entity
{
public: 
    int x, y;

    Entity(int x, int y)
    {
        Entity* e = this;                // 'this' points to the object being constructed
        this->x = x;                     // sets the current object's x
        this->y = y;

        Entity& e_ref = *this;           // dereference 'this' to get reference to current object

        PrintEntity(*this);              // pass the current object to external function

        // delete this;                  // NEVER DO THIS in constructor (undefined behavior!)
    }

    int GetX() const
    {
        const Entity& e = *this;         // 'this' is const Entity* inside const function
        return x;                        // same as return this->x;
    }
};


## The `this` Keyword in C++ – Pointer to the Current Object Instance

`this` is a **special pointer** automatically passed to **every non-static member function** (including constructors and destructors).  
It always points to **the current object** you're working inside.

### Important Rules (from your code)

- `this` only exists **inside member functions** (methods of the class)  
- It is **never** available in static functions or outside the class
- Type of `this`:  
  - In non-const member functions → `Class* const this` (pointer to current object, can't reassign what it points to)  
  - In const member functions → `const Class* const this` (can't modify members through it)

### Your Code Explained Line by Line

```cpp
class Entity
{
public: 
    int x, y;

    Entity(int x, int y)
    {
        // 'this' is a pointer to the object being constructed right now
        Entity* e = this;                    // valid — e now points to current Entity

        // Entity* const& e = this;          // also valid — reference to const pointer
        // (but almost never needed)

        // e->x = x;                         // works — same as below
        this->x = x;                         // sets current object's x (disambiguates param x)
        this->y = y;                         // same for y

        // (*this).x = x;                    // equivalent to this->x (dereference + arrow)
        // But this-> is cleaner & more common
    }

    int GetX() const
    {
        // Entity* e = this;                 // ERROR! Can't assign const pointer to non-const
        const Entity* e = this;              // correct — in const function, this is const Entity*

        // e->x = 5;                         // ERROR! Can't modify through const pointer

        return x;                            // returns current object's x (this->x)
        // or return this->x;                 // same thing
    }
};

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant